var index;

var table;
var form;

$(document).ready(function () {
    $('#page-toolbar-content').show();

    layui.use(['table', 'form'], function () {

        table = layui.table;
        form = layui.form;
        form.render();

        getAllButton(currUrl).done(function (returnData) {
            createButton(returnData);
            initButtonEvent();
            initTable();
        });

    });

});

function initButtonEvent() {

    var $ = layui.$, active = {
        
        search: function () {

            var deviceName = $('#deviceName');
            var deviceNo = $('#deviceNo');
            var deviceCheckStatus = $('#deviceCheckStatus');

            // 执行重载
            table.reload('repair-table-content', {
                page: {
                    curr: 1
                }
                , where: {
                    deviceName: deviceName.val(),
                    deviceNo: deviceNo.val(),
                    deviceCheckStatus: deviceCheckStatus.val()
                }
            });

            $('.layui-table-page').css('text-align', 'right');
            $('.layui-table-page').css('padding-right', '20px');

        }
    };

    $('.menu-btn-group .layui-btn, .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

}

function initTable() {

    table.render({
        elem: '#repair-table-content'
        , url: apiHostUrl + '/admin/repair/queryRepair'
        , headers: {Authorization: token}
        , response: {
            statusName: 'errorCode'
            , statusCode: 0
            , msgName: 'errorMsg'
            , countName: 'totalCount'
            , dataName: 'returnData'
        }
        , cellMinWidth: 50
        , limit: 15
        , limits: [15]
        , cols: [[
            {type: 'checkbox'}
            , {field: 'deviceCheckId', width: 130, title: 'ID'}
            , {field: 'deviceNo', width: 200, title: '设备编号'}
            , {field: 'deviceName', width: 200, title: '设备名称'}
            , {field: 'deviceCheckDetail',  title: '检修说明'}
            , {field: 'deviceCheckTime', width: 180, title: '检修时间'}
            , {field: 'createUserName', width: 150, title: '采购人员'}
            , {field: 'repairUserName', width: 150, title: '维修人员'}
            , {field: 'deviceCheckStatusString', width: 90, title: '设备状态'}
        ]]
        , page: {
            layout: ['prev', 'page', 'next', 'skip', 'limit', 'count']
        }
        , id: 'repair-table-content'
    });

    $('.layui-table-page').css('text-align', 'right');
    $('.layui-table-page').css('padding-right', '20px');

}
